
function Get-Certificate
{
	param([string]$certStore)

	$store = new-object -com "CAPICOM.Store"

	$store.Open(1,"$certStore",130)

	$store.Certificates 
	
	$Store.Close()
}

function Remove-Certificate
{
	param([String]$certName,[String]$certStore)
	
	$store = new-object -com "CAPICOM.Store"

	$store.Open(1,"$certStore",130)

	$certToDelete  =( $store.Certificates | where { $_.SubjectName -like "*$certName*" })
	
	if ($certToDelete -is [Object])
	{
		if ($certToDelete.PrivateKey -is [Object])
		{
			$certToDelete.PrivateKey.Delete()
			$store.Remove($certToDelete)
		}
	}
	
	$Store.Close()
}
function Add-Certificate
{
	param([String]$certPath,[String]$certStore)
	
	$store = new-object -com "CAPICOM.Store"

	$store.Open(1,"$certStore",130)

	write $certPath

	$store.Load($certPath)
	
	$store.Close()

}

function set-CertificateSecurity
{
	param([String]$certName)
	
	$keypath = $env:ProgramData + "\Microsoft\Crypto\RSA\MachineKeys\"
	
	$store = new-object -com "CAPICOM.Store"

	$store.Open(1,"my",130)

	$certToSecure  =  $store.Certificates | where { $_.subjectname -like "*$certName*" }

   	$keyname = $certToSecure.PrivateKey.UniqueContainerName


	if ($keyname -is [Object])
	{


	  	icacls $keypath$keyname /grant networkservice:RX	
	}
  	$store.Close()
}

$p = (get-location).Path
remove-certificate "adatum" "root"
add-certificate $p\certs\adatum.sst "root"
add-certificate $p\certs\dinnernow.pfx "my"
set-certificatesecurity "dinnernow" "my"

